<template>
	<view class="order-container c-container table-container">
		<navigator class="fixed-right" v-if='order_id' :url="'/pages/order/detail/detail?order_id='+order_id">
			<text>查看定单详情</text>
		</navigator>
		<view class="c-row-box">
			<view class="c-row-head">
				<view class="c-row-title">添加利润</view>
			</view>
			<view class="c-row-item">
				<view class="c-row c-row-list">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title"><text style="color: red;">*</text>收入总额</view>
						<view class="col-text">
							<input type="digit"  v-model="profitForm.income_fee" :disabled="isPreview" @input="profitChange"  placeholder="请输入" />
						</view>
					</view>
				</view>
				<view class="c-row c-row-list">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title"><text style="color: red;">*</text>应付酒店</view>
						<view class="col-text">
							<input  type="digit" v-model="profitForm.pay_hotel_fee" :disabled="isPreview" @input="profitChange"  placeholder="请输入" />
						</view>
					</view>
				</view>
				
				<view class="c-row c-row-list">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title" style="min-width: 91px;"><text style="color: red;">*</text>平台渠道费用</view>
						<view class="col-text">
							<input type="digit"  v-model="profitForm.source_fee" :disabled="isPreview" @input="profitChange" placeholder="请输入" />
						</view>
					</view>
				</view>
				<view class="c-row c-row-list">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title" style="min-width: 91px;"><text style="color: red;">*</text>个人渠道费用</view>
						<view class="col-text">
							<input  type="digit" v-model="profitForm.person_source_fee" @input="profitChange" :disabled="isPreview"
								placeholder="请输入" />
						</view>
					</view>
				</view>
                <view class="c-row c-row-list">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title"><text style="color: red;">*</text>酒水成本</view>
						<view class="col-text">
							<input type="digit"  v-model="profitForm.wine_cost" :disabled="isPreview" @input="profitChange" placeholder="请输入" />
						</view>
					</view>
				</view>
                <view class="c-row c-row-list">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title"><text style="color: red;">*</text>赠送成本</view>
						<view class="col-text">
							<input  type="digit" v-model="profitForm.free_cost" @input="profitChange" :disabled="isPreview"
								placeholder="请输入" />
						</view>
					</view>
				</view>
                <view class="c-row c-row-list">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title" style="min-width: 91px;"><text style="color: red;">*</text>婚庆签单额</view>
						<view class="col-text">
							<input type="digit"  v-model="profitForm.wedding_cost" :disabled="isPreview" @input="profitChange" placeholder="请输入" />
						</view>
					</view>
				</view>
                <view class="c-row c-row-list">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title"><text style="color: red;">*</text>婚宴净利</view>
						<view class="col-text">
							<input type="digit"  v-model="profitForm.banquet_profit"  :disabled="isPreview" @input="profitChange"
								placeholder="请输入" />
						</view>
					</view>
				</view>
                <view class="c-row c-row-list remark-line">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title remark">备注</view>
						<!-- <view class="col-text">
							<input  v-model="profitForm.banquet_profit_remark" :disabled="isPreview"  placeholder="请输入" />
						</view> -->
					</view>
				</view>
				<view class="textarea">
					<textarea class="area" name="" id=""  v-model="profitForm.banquet_profit_remark" :disabled="isPreview"></textarea>
				</view>
			</view>
		</view>
        <view class="reminder">
			<view class="title"><text style="color: red;margin-right: 5px;">*</text>提 示 :</view>
			<view class="rules">婚宴净利 = 收入总额 - 应付酒店 - 平台渠道费用 - 个人渠道费用 - 酒水成本 - 赠送成本 - 婚庆签单额</view>
		</view>
		<!-- 底部栏-创建 -->
		<view class="footer-box" v-if="confirm_id == 0">
			<view class="footer-action-btn" >
				<button size="mini" @click="saveprofit()">提交</button>
			</view>
		</view>
		<divider title="审核流程" v-if="sequenceData.length > 0 || confirmSteps.length > 0"></divider>
		<!-- 新版审批流程————创建  -->
		<template v-if="sequenceData.length > 0">
			<steps-create :stepList="sequenceData"></steps-create>
		</template>
		<!-- 新版审核流程————查看 -->
		<template v-if="confirmSteps.length > 0">
			<steps-look :stepList="confirmSteps"></steps-look>
			<footer-action-btn :btnValue="buttons" :transferForm="transferForm" @clearTransferForm="clearTransferForm" :isShowAddonsBtn="false"
				:isShowBtn="options.api?false:true" @resubmit='resubmits' @transferSubmit="transferSubmit">
			</footer-action-btn>
		</template>


	</view>
</template>

<script>
	import divider from '@/component/divider/divider.vue'

	import stepsLook from '@/component/steps-look/steps-look.vue'; // 新版审核流程
	import stepsCreate from '@/component/steps-create/steps-create.vue';
	import auditProcess from '@/component/auditProcess.vue';
	import footerActionBtn from '@/component/footer-action-btn/footer-action-btn.vue';

	import request from '@/utils/request.js';
	export default {
		components: {
			divider,
			stepsCreate,
			stepsLook,
			auditProcess,
			footerActionBtn
		},
		data() {
			return {
				confirm_id: 0,
				order_id: 0,
                profitForm:{
                   pay_hotel_fee: '',
                   income_fee: '',
                   source_fee: '',
                   person_source_fee: '',
                   wine_cost: '',
                   free_cost: '',
                   wedding_cost: '',
                   banquet_profit: '',
                   banquet_profit_remark: ''
                },
				isPreview: false,
				options: '',

				confirmList: [], // 老版审核列表
				buttons: [],
				confirm: {}, //审批信息

				
				transferForm: {
					realname: '',
					user_id: ''
				},
				sequenceData: [], // 新版审核
				confirmSteps: [], // 新版审核
			}
		},
		onLoad(options) {
            console.log(options);
			// 添加 从订单详情页添加子项目 包含order_id
			if (options.params) {
				this.options = JSON.parse(options.params);
                this.order_id = this.options.order_id;
                this.getConfirmSequence()
            }
            // 审核查看 lookApproval是个对象，包含的id是审核的编号，order_id要从审核详情中获取
			if (options.lookApproval) {
				console.log(options)
				this.options = JSON.parse(options.lookApproval)
				this.isPreview = true;
                this.confirm_id = this.options.id;
                //查看信息
				this.getDetail(this.options)
				this.confirm_id = this.options.id;
				// 审核流程--查看
				this.getConfirmList()
			}

			// 查看 从查看订单审核中进入只有id，这个id是审核的编号，order_id要从审核详情中获取
			if (options.id) {
				console.log(options);
				this.options = options;
				this.isPreview = true;
                this.confirm_id = this.options.id;
                //查看信息
				this.getDetail(this.options)
				this.confirm_id = this.options.id;
				// 审核流程--查看
				this.getConfirmList()
			}
		},
		computed: {
			
		},
		methods: {
			 // 利润发生变化自动计算
			profitChange() {
				let that = this;
				let income_fee = that.profitForm.income_fee>0 ? parseFloat(that.profitForm.income_fee) : 0;
				let pay_hotel_fee = that.profitForm.pay_hotel_fee>0 ? parseFloat(that.profitForm.pay_hotel_fee) : 0;
				let source_fee = that.profitForm.source_fee>0 ? parseFloat(that.profitForm.source_fee) : 0;
				let person_source_fee = that.profitForm.person_source_fee>0 ? parseFloat(that.profitForm.person_source_fee) : 0;
				let wine_cost = that.profitForm.wine_cost>0 ? parseFloat(that.profitForm.wine_cost) : 0;
				let free_cost = that.profitForm.free_cost>0 ? parseFloat(that.profitForm.free_cost) : 0;
				let wedding_cost = that.profitForm.wedding_cost>0 ? parseFloat(that.profitForm.wedding_cost) : 0;
				that.profitForm.banquet_profit = (
					income_fee -
					pay_hotel_fee -
					source_fee -
					person_source_fee -
					wine_cost -
					free_cost -
					wedding_cost
				).toFixed(2);
			},
            getDetail(e) {
				let that = this
				let data
				if (e.is_comment) {
					data = {
						id: e.id,
						is_comment: e.is_comment,
						type: e.user ? 'confirm_user' : '',
					}

				} else {
					data = {
						id: e.id,
						type: e.user ? 'confirm_user' : '',
					}
				}
				request.request({
					url: '/h5/order.confirm/detail',
					showLoading: true,
					token: true,
					data: data,
					success(res) {
						if (res.code == 200) {
							that.profitForm = res.data.detail.orderProfit;
							that.buttons = res.data.buttons;
							that.order_id = res.data.detail.orderProfit.order_id;
							console.log(that.order_id);
							//判断是否可以编辑
							that.buttons.map((item, index) => {
								if (item.id == 'update') {
									that.isPreview = false;
								}
							})
						}
					},
					fail(e) {
						uni.showToast({
							icon: "none",
							title: "服务器开小差去啦~",
						})
					}
				})
			},

		//  查看详情审核流程
			getConfirmList() {
				let that = this
				request.request({
					url: "/api/order.audit/getAuditResult",
					showLoading: true,
					token: true,
					data: {
						id: that.confirm_id
					},
					success(res) {
						that.confirmSteps = res.data.process
					}
				})
			},
			//创建订单时的审核流程
			getConfirmSequence(e) {
				let that = this
				request.request({
					url: "/api/order.audit/getAuditProcess",
					showLoading: true,
					token: true,
					data: Object.assign({
						order_id: that.order_id,
						audit_type: 'order_profit',
						company_id: 0
					}),
					success(res) {
						if (res.code == 200) {
                            console.log(res.data);
							that.sequenceData = res.data.process
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg
							})
						}
					}
				})
			},
             // 提交创建利润
           saveprofit(){
			  let that = this;
			  if(!that.verifyFn()) return;
			  if(!that.profitForm.banquet_profit){
                  uni.showToast({
						icon: 'none',
						title: '信息填写不完整'
					});
					return;
			  }
            request.request({
                        url: "/api/audit.profit/create",
                        showLoading: true,
                        token: true,
                        data: Object.assign({
                            order_id: that.order_id,
                            orderProfit: that.profitForm
                        }),
                        success(res) {
                            if (res.code == 200) {
								uni.navigateBack({
									delta: 1
								});
                            } else {
                                uni.showToast({
                                    icon: 'none',
                                    title: res.msg
                                })
                            }
                        }
                    })
			},
			// 编辑提交
			editFn(api, id) {
				let that = this;
				if (!that.verifyFn()) return
				let params = {};
				params['order_confirm_id'] = that.confirm_id;
				params['order_id'] = that.order_id;
				params['is_new'] = 1;
				params['orderProfit'] = Object.assign(that.profitForm);
				request.request({
					url: api,
					showLoading: true,
					token: true,
					data: params,
					success(res) {
						if (res.code == 200) {
							uni.redirectTo({
								url: "/pages/order/index"
							})
							uni.showToast({
								icon: "none",
								title: res.msg
							})
						} else {
							uni.showToast({
								icon: "none",
								title: res.msg
							})
						}
					}
				})
			},
			//底部评论按钮
			resubmits(item) {
				if (item.label == '同意' || item.label == '拒绝' || item.label == '评论') {
					this.commentFn(item);
				} else if (item.label == '转交') {
					uni.navigateTo({
						url: '../../../component/search/search?type=' + 'changeUser'
					});
				} else {
					this.editFn(item.api, item.id);
				}
			},

			// 编辑提交
			commentFn(e) {
				let that = this;
				let url = '';
				if (e.label == '拒绝' && that.confirmList.length == 0) {
					url = '/api/order.audit/reject';
				} else if (e.label == '同意' && that.confirmList.length == 0) {
					url = '/api/order.audit/accept';
				} else {
					url = e.api;
				}
				request.request({
					url: url,
					showLoading: true,
					token: true,
					data: {
						id: that.confirm_id,
						content: e.content ? e.content : '',
						image: e.image ? e.image : ''
					},
					success(res) {
						if (res.code == 200) {
							uni.showToast({
								icon: 'none',
								title: res.msg
							});
							that.getDetail(that.options);
							that.getConfirmList();
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg
							});
						}
					}
				});
			},
			// 校验信息
			verifyFn(){
				let profitForm = this.profitForm;
				let bool = true;
				if(profitForm.income_fee == ''){
					uni.showToast({
							icon: 'none',
							title: '请填写收入总额'
						});
						bool = false
						return false
				}
				if(profitForm.pay_hotel_fee == ''){
                    uni.showToast({
						icon: 'none',
						title: '请填写应付酒店'
					});
					bool = false
					return false
				}
				if(profitForm.source_fee == ''){
					uni.showToast({
							icon: 'none',
							title: '请填写平台渠道费用'
						});
						bool = false
						return false
				}
				if(profitForm.person_source_fee == ''){
						uni.showToast({
							icon: 'none',
							title: '请填写个人渠道费用'
						});
						bool = false
						return false
				}
				if(profitForm.wine_cost == ''){
					uni.showToast({
							icon: 'none',
							title: '请填写酒水成本'
						});
						bool = false
						return false
				}
				if(profitForm.free_cost == ''){
					uni.showToast({
							icon: 'none',
							title: '请填写赠送成本'
						});
						bool = false
						return false
				}
				if(profitForm.wedding_cost == ''){
						uni.showToast({
							icon: 'none',
							title: '请填写婚庆签单额'
						});
						bool = false
						return false
				}
				if(profitForm.banquet_profit == ''){
                    uni.showToast({
							icon: 'none',
							title: '请填写婚宴净利'
						});
						bool = false
						return false
				}
				return bool
			}
			
        },
       
	}
</script>

<style lang="less">
	uni-page-body {
		padding-top: 0;
	}

	@import url('../../order/createOrder/components/common.less');
	@import url('../../order/components/common.less');

	.table-container {
		padding-bottom: 120upx;

		.footer-box {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			padding: 0 16px;
			display: flex;
			align-items: center;
			height: 100upx;
			font-size: 28upx;
			background-color: #FFFFFF;
			border-top: 1px solid #dedede;
			z-index: 9;

			.footer-title {
				flex: 0 15%;
			}

			.footer-action-btn {
				flex: 1;
				text-align: end;

				button {
					background-color: #007AFF;
					color: #FFFFFF;
				}

				button+button {
					margin-left: 5px;
				}

			}

		}
		
	}
	.remark{
		padding-left: 10px !important;
		box-sizing: border-box !important;
		max-width: 50px;
	}
	.reminder{
		margin-top: 20px;
		font-size: 14px;
		.title{
			padding-left: 10px;
			box-sizing: border-box;
			margin-bottom: 10px;
		}
	    .rules{
			padding-left: 22px;
			box-sizing: border-box;
			color: #555;
			font-size: 12px;
		}
		
	}
	.remark-line{
		border-bottom: none!important;
	}
	.textarea{
		padding: 0 10px;
		box-sizing: border-box;
		.area{
			width: 100%;
			height: 100px;
			border: 1px solid #ebedf0;
			border-radius: 5px;
			padding: 10px;
			box-sizing: border-box;
			font-size: 12px;
			color: #555;
		}
	}
</style>
